package com.JUtils.ssh;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.util.Properties;

public class SSHUtil {

    private final static String S_PATH_FILE_PRIVATE_KEY = "/Users/liumingyue/.ssh/id_rsa";
    private final static String S_PATH_FILE_KNOWN_HOSTS = "/Users/liumingyue/.ssh/known_hosts";
    private final static String S_PASS_PHRASE = "";
    private final static int LOCAl_PORT = 3306;
    private final static int REMOTE_PORT = 3306;
    private final static int SSH_REMOTE_PORT = 60022;
    private final static String SSH_USER = "web";
    private final static String SSH_PASSWORD = "";
    private final static String SSH_REMOTE_SERVER = "101.200.89.192";
    private final static String MYSQL_REMOTE_SERVER = "123.mysql.com";

    private Session sesion; //represents each ssh session

    public void closeSSH ()
    {
        sesion.disconnect();
    }

    public SSHUtil () throws Throwable
    {

        JSch jsch = null;

        jsch = new JSch();
        jsch.setKnownHosts(S_PATH_FILE_KNOWN_HOSTS);

        sesion = jsch.getSession(SSH_USER, SSH_REMOTE_SERVER, SSH_REMOTE_PORT);

        //sesion.setPassword("111111");

       /* Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        sesion.setConfig(config);*/

        sesion.connect(); //ssh connection established!

        //by security policy, you must connect through a fowarded port
        sesion.setPortForwardingL(LOCAl_PORT, MYSQL_REMOTE_SERVER, REMOTE_PORT);

    }

    public static void main(String[] args)throws Throwable{
        new SSHUtil();
    }
}
